package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.collect.ListUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.R;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.viewModel.impl.RecommendNewComicViewModel;
import com.qire.manhua.model.vo.HomeRecommendNewComicItemVO;
import com.qire.manhua.view.attributeBinder.attribute.ImageAttribute;

import java.util.List;

public class RecommendNewComicService extends DataService {

    private IPageDataDAL pageData = WarehouseFactory.create(this, IPageDataDAL.class);

    private final RecommendNewComicViewModel viewModel;

    public RecommendNewComicService(RecommendNewComicViewModel viewModel) {
        this.viewModel = viewModel;
    }

    // 更新数据
    public void loadPageData() {
        pageData.fetchRecommendNewComicPageData(viewModel.pageNo());
    }

    @StrategyAnnotation(event = Url.v5_newComic)
    public void fetchRecommendNewComicPageDataCallBack(List<HomeRecommendNewComicItemVO> dataVOList, boolean isCache) {

        viewModel.refreshLayoutFinish(300);
        // 没有更多数据了
        if (ListUtil.isEmpty(dataVOList)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        //缓存并不会增加页面，所以第一次进入缓存会先使用不增加页面，非缓存进入时页面怎加，清空缓存记录重新插入。
        if (viewModel.isFirstPage()) {
            viewModel.clearNewComicList();
        }

        for(HomeRecommendNewComicItemVO dataVO : dataVOList){
            //不确定是否需要过滤重复数据，如果发现有重复数据在此处添加判断过滤

            ImageAttribute imgAttr = new ImageAttribute(dataVO.cover_img, R.drawable.app_placeholder220x290);
            int comicId = dataVO.book_id;
            String comicName = dataVO.book_name;
            String comicAuthor = "作者：" + dataVO.book_author;
            String comicBrief = dataVO.short_title;
            String describe = dataVO.toDescribe();
            String[] labels = ListUtil.isEmpty(dataVO.tags) ? new String[0] : dataVO.tags.toArray(new String[0]);

            viewModel.addNewComicItem(imgAttr, comicId, comicName, comicAuthor, comicBrief, describe, labels);
        }

        viewModel.refreshNewComicAdapter();
        if (!isCache) {
            viewModel.nextPage();
        }

        viewModel.refreshErrorDataUI();

    }




}
